package com.example.fw;

import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;

public class HibernateHelper extends HelperBase{

	public HibernateHelper(ApplicationManager manager) {
		super(manager);
		}

	public String getLastGroupId() {	
		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
		session.beginTransaction();
		return session.createQuery("select max(id)from GroupObject").uniqueResult().toString();
	}
	
	public String getLastContactId() {	
		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
		session.beginTransaction();
		return session.createQuery("select max(id)from ContactObjects").uniqueResult().toString();
	}
	
	public Groups getGroups() {
		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
		session.beginTransaction();
		Query query =  session.createQuery("from GroupObject go where go.id =null");
		Groups dbGroups = 	new Groups((List<GroupObject>) query.list());
		session.close();
		return dbGroups;
	}

	public Contacts getContacts() {
		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
		session.beginTransaction();
		List<ContactObjects> contacts = (List<ContactObjects>) session.createQuery("from ContactObjects").list();
		Contacts dbContacts = new Contacts(contacts);
		session.close();
		return dbContacts;
	}
}